package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.badoo.mobile.util.WeakHandler;
import com.jwgf.common.ViewCache;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.DirectCurrentStatus;

import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class MachineStateDirectCurrentFragment extends BaseFragment implements View.OnClickListener {
    private LineChartView lineChartView;

    private Button voltage_btn;
    private Button current_btn;
    private Button temperature_btn;

    private TextView recent_value;

    private WeakHandler handler;
    private Runnable runnable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_machine_state_direct_current);
        lineChartView = v.findViewById(R.id.line_chart_view);
        voltage_btn = v.findViewById(R.id.voltage_btn);
        voltage_btn.setOnClickListener(this);
        current_btn = v.findViewById(R.id.current_btn);
        current_btn.setOnClickListener(this);
        temperature_btn = v.findViewById(R.id.temperature_btn);
        temperature_btn.setOnClickListener(this);

        recent_value = v.findViewById(R.id.recent_value);

        lineChartView.setInteractive(true);
        lineChartView.setZoomEnabled(false);
        lineChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

        setButtonSelected(voltage_btn);
        updateChart();

        handler = new WeakHandler();
        runnable = new Runnable() {
            @Override
            public void run() {
                updateChart();
                handler.postDelayed(runnable, 60 * 1000);
            }
        };
        handler.postDelayed(runnable, 60 * 1000);

        return v;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            handler.post(runnable);
        } else {
            handler.removeCallbacks(runnable);
        }
    }

    private void updateChart() {
        if (voltage_btn.isActivated()) {
            recent_value.setText(DirectCurrentStatus.getInstance().getRecentVoltage());
            lineChartView.setLineChartData(DirectCurrentStatus.getInstance().getVoltageData());
            Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
            viewport.bottom = 0;
            viewport.top = 400;
            lineChartView.setMaximumViewport(viewport);
            viewport.left = 30;
            viewport.right = 60;
            lineChartView.setCurrentViewport(viewport);
        } else if (current_btn.isActivated()) {
            recent_value.setText(DirectCurrentStatus.getInstance().getRecentCurrent());
            lineChartView.setLineChartData(DirectCurrentStatus.getInstance().getCurrentData());
            Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
            viewport.bottom = 0;
            viewport.top = 40;
            lineChartView.setMaximumViewport(viewport);
            viewport.left = 30;
            viewport.right = 60;
            lineChartView.setCurrentViewport(viewport);
        } else {
            recent_value.setText(DirectCurrentStatus.getInstance().getRecentTemperature());
            lineChartView.setLineChartData(DirectCurrentStatus.getInstance().getTemperatureData());
            Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
            viewport.bottom = 0;
            viewport.top = 60;
            lineChartView.setMaximumViewport(viewport);
            viewport.left = 30;
            viewport.right = 60;
            lineChartView.setCurrentViewport(viewport);
        }
    }

    private void setButtonSelected(View button) {
        voltage_btn.setActivated(false);
        current_btn.setActivated(false);
        temperature_btn.setActivated(false);
        button.setActivated(true);
    }

    @Override
    public void onClick(View v) {
        setButtonSelected(v);
        updateChart();
    }
}
